package maxHeap2;

import java.util.Arrays;
import java.util.Random;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/19 8:51
 * 测试最大堆的heapify
 */
public class MainTest {
    private static double testHeap(Integer[] testData, boolean isHeapify){

        long startTime = System.nanoTime();

        MaxHeap<Integer> maxHeap;
        if(isHeapify) { //使用heapify建堆
            maxHeap = new MaxHeap<>(testData);
        } else{ //普通---添加建堆
            maxHeap = new MaxHeap<>(testData.length);
            for(int num: testData) {
                maxHeap.add(num);
            }
        }

        int[] arr = new int[testData.length];
        for(int i = 0 ; i < testData.length ; i ++) {
            arr[i] = maxHeap.extractMax();
        }

        //判断是不是有序
        for(int i = 1 ; i < testData.length ; i ++) {
            if(arr[i-1] < arr[i]) {
                throw new IllegalArgumentException("Error");
            }
        }
        System.out.println("Test MaxHeap completed.");

        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }



    public static void main(String[] args) {

        int n = 10000000;

        Random random = new Random();
        Integer[] testData1 = new Integer[n];
        for(int i = 0 ; i < n ; i ++) {
            testData1[i] = random.nextInt(Integer.MAX_VALUE);
        }

        Integer[] testData2 = Arrays.copyOf(testData1, n);

        double time1 = testHeap(testData1, false);
        System.out.println("Without heapify: " + time1 + " s");

        double time2 = testHeap(testData2, true);
        System.out.println("With heapify: " + time2 + " s");
    }
}
